package com.cskaoyan.gateway.controller.user;

import cn.hutool.json.JSONUtil;
import com.cskaoyan.gateway.bean.login.bo.LoginPostBo;
import com.cskaoyan.gateway.bean.login.vo.LoginGetVo;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.ILoginService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.userLoginRequest;
import com.mall.user.dto.UserLoginResponse;
import com.mall.user.dto.UserVerifyResponse;
import com.mall.user.intercepter.TokenInterceptor;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.LinkedHashMap;
import java.util.Map;

import static com.mall.user.intercepter.TokenInterceptor.ACCESS_TOKEN;
import static com.mall.user.intercepter.TokenInterceptor.USER_INFO_KEY;

/**
 * @author wangdezhao
 * @date 2021/6/18
 * @description
 */

@RestController
@RequestMapping("/user")
public class LoginController {

    @Reference
    ILoginService loginService;


    /**
     * 用户登录
     */
    @Anoymous
    @PostMapping("/login")
    public ResponseData loginPost(@RequestBody LoginPostBo loginBo, HttpServletResponse httpServletResponse) {
        // 取出参数
        String password = loginBo.getUserPwd();
        String userName = loginBo.getUserName();
        // 新建request对象
        userLoginRequest userLoginRequest = new userLoginRequest();
        userLoginRequest.setPassword(password);
        userLoginRequest.setUserName(userName);

        // 得到response对象
        UserLoginResponse response = loginService.loginPost(userLoginRequest);

        if (SysRetCodeConstants.SUCCESS.getCode().equals(response.getCode())) {
            String token = response.getToken();
            Cookie cookie = CookieUtil.genCookie("access_token", token, "/", 86400);
            CookieUtil.setCookie(httpServletResponse, cookie); // 将token放入cookies中,之后验证登录时[loginGet()方法]要取出验证
            return new ResponseUtil().setData((response)); // 成功返回对象
        }
        return new ResponseUtil().setErrorMsg(response.getMsg()); // 失败返回状态码
    }


    /**
     * 验证用户登陆: 取出cookies中的access_token做验证登录
     */
    @Anoymous
    @GetMapping("/login")
    public ResponseData loginGet(HttpServletRequest httpServletRequest) {
        LoginGetVo loginGetVo = new LoginGetVo();
        Map<String, String> map = new LinkedHashMap<>();

        // 新建request对象,传入service层
        userLoginRequest request = new userLoginRequest();

        String token = CookieUtil.getCookieValue(httpServletRequest, ACCESS_TOKEN); // 取出header中的token数据




        if (token == null) { // 如果是第一次访问网页,先放行,
            return new ResponseUtil().setErrorMsg(SysRetCodeConstants.TOKEN_VALID_FAILED.getMessage());
        }

        request.setUserName(token); // 传入token
        request.setPassword("123"); // 为了之后requestCheck()方法不受密码为空的影响,随意填的,因为这里并不需要密码
        UserVerifyResponse response = loginService.loginGet(request);

        if (SysRetCodeConstants.SUCCESS.getCode().equals(response.getCode())) { // token校验成功
            loginGetVo.setUid(response.getUid());
            loginGetVo.setUsername(response.getUsername());
//            map.put("uid",response.getUid().toString());
//            map.put("username",response.getUsername());
//            String jsonStr = JSONUtil.toJsonStr(map);
//            httpServletRequest.setAttribute(USER_INFO_KEY, jsonStr); //保存username的信息后续要用
            return new ResponseUtil().setData(loginGetVo);
        }
        return new ResponseUtil().setErrorMsg(SysRetCodeConstants.TOKEN_VALID_FAILED.getMessage());
    }

    /**
     * 退出登录
     */
    @Anoymous
    @GetMapping("/loginOut")
    public ResponseData loginOut(HttpServletRequest request, HttpServletResponse response) {

        Cookie cookie = CookieUtil.genCookie("access_token", null, "/", 0);

        CookieUtil.setCookie(response,cookie);

        return new ResponseUtil<>().setData(null);
    }



}
